import { Component, OnInit } from '@angular/core';
import { CustomerService } from './customer-services/customer.service';
import { ActivatedRoute, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { UserService } from '../shared/services/user.service';

@Component({
  selector: 'app-customer',
  templateUrl: './customer.component.html',
  styleUrls: ['./customer.component.css']
})
export class CustomerComponent implements OnInit {
  display: boolean = false;
  newPassword1:string = "";
  newPassword2:string = "";
  user:any = {};
  constructor(private customerService: CustomerService, 
    private userService:UserService,
    private route:ActivatedRoute,
    private router:Router,
    private translate:TranslateService) { }

  ngOnInit() {
    this.getCurrentUser();
    
   
  }

  //logout
  onLoggedout(){
    this.userService.logout().subscribe(
      (res)=>{
        this.router.navigate(['/login']);
      },
      (err)=>{
        console.log(err);
        // this.router.navigate(['/login']);
      }
    )
  }

  
  resetPassword(){
    if(this.newPassword1 === this.newPassword2){
      this.userService.patch( this.user.id, {password:this.newPassword1}).subscribe(
        (res) => {
          console.log(res);
          alert("update password success!");
          this.display =false;
        },
        (err) => {
          alert("update password fail!");
        }
      );
    }else{
      alert("password not matched!");
    }

  }

  getCurrentUser(){
    this.userService.getCurrentUser().subscribe((user)=>{
      this.user = user;
      this.router.navigate(['/customer/shared/customer-property-grid',user.id]);
    });
  }

  changeLang(language: string) {
    this.translate.use(language);
}

}
